
package org.apache.osgimaker.analyse.algorithm.classfile;

/**
 * Abstract supper class of all reference constants.
 * 
 */
public abstract class RefConstant extends Constant {
  private final int _classIndex;
  private final int _nameAndTypeIndex;

  /**
   * Creates an instance for the specified class, name, and type.
   * @param pool Constant pool. Needed for resolving references.
   * @param classIndex Index of {@link ClassConstant}.
   * @param nameAndTypeIndex Index of {@link NameAndTypeConstant}.
   */
  public RefConstant(Constant[] pool, int classIndex, int nameAndTypeIndex) {
    super(pool);
    _classIndex = classIndex;
    _nameAndTypeIndex = nameAndTypeIndex;
  }

  /** Returns the class constant. */
  public ClassConstant getClassConstant() {
    return (ClassConstant) getConstant(_classIndex);
  }

  /** Returns the name-and-type constant. */
  public NameAndTypeConstant getNameAndType() {
    return (NameAndTypeConstant) getConstant(_nameAndTypeIndex);
  }

  /** Pretty printing. Will be used by <tt>toString</tt> of the subclasses. */
  protected String toString(String constantType) {
    return constantType + ": Class = " + getClassConstant().getName()
                        + ", Name = " + getNameAndType().getName()
                        + ", Descriptor = " + getNameAndType().getDescriptor();
  }
} //class